package com.kg.sdk.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.common.network.NetworkConfigData;
import com.common.network.NetworkResponse;
import com.common.network.RequestListener;
import com.example.kpgamesdk.R;
import com.kg.sdk.SharedPreferencesCtrl;
import com.kg.sdk.network.KPPostRequest;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.KPNetworkData;
import com.kg.sdk.view.TCMLoadInputLayout;

import org.json.JSONException;
import org.json.JSONObject;

public class KPLoginActivity extends KPCommonActivity implements View.OnClickListener {
    private String accountNoNull;
    private String passwordNoNull;
    private TCMLoadInputLayout liAccount;
    private TCMLoadInputLayout liPassword;
    private Button btnLogin;
    private TextView btnRegist;
    private TextView btnResetPassword;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        View view = LayoutInflater.from(this).inflate(R.layout.act_login_layout,null);
        setContentView(view);
        accountNoNull = getString(R.string.accountNoNull);
        passwordNoNull = getString(R.string.passwordNoNull);
        liAccount = (TCMLoadInputLayout)findViewById(R.id.liAccount);
        liPassword = (TCMLoadInputLayout)findViewById(R.id.liPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnResetPassword = (TextView)findViewById(R.id.btnResetPassword);
        btnResetPassword.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        btnRegist = (TextView) findViewById(R.id.tvTopRight);
        btnRegist.setOnClickListener(this);
        liAccount.setEditText("qz123456");
        liPassword.setEditText("123456");
       // requestPermissions();
       // startFloagService();
      //  SystemPermission.requestPermissions(this,new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW});

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.btnLogin){
            mWaitingDialog.showDialog();
            login();
        }else if(id == R.id.tvTopRight){
            Intent intent = new Intent();
            intent.setClass(KPLoginActivity.this, KPRegistActivity.class);
            startActivityForResult(intent,KPNetworkData.LOGIN_REQUEST);
        }else if(id == R.id.btnResetPassword){
            Intent intent = new Intent();
            intent.setClass(KPLoginActivity.this, KPResetPwdActivity.class);
            startActivityForResult(intent,KPNetworkData.LOGIN_REQUEST);
        }
    }

    private void login(){
        String account = liAccount.getInputText();
        String password = liPassword.getInputText();
        if(account.length() == 0){
            CommonUtil.showToast(this,accountNoNull);
            return;
        }
        if(password.length() == 0){
            CommonUtil.showToast(this,passwordNoNull);
            return;
        }
        String url = KPNetworkData.LOGIN;
        RequestListener listener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," logni success is "+data);
                if(code == KPNetworkData.SUCCESS){
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        CommonUtil.getXSID(result);
                        final String auth_code = jsonObject.getString(KPNetworkData.AUTH_CODE_KYE);
                        final String auth_key = jsonObject.getString(KPNetworkData.AUTH_KYE);
                        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(KPLoginActivity.this,KPNetworkData.KPGAME);
                        sharedPreferencesCtrl.put(KPNetworkData.AUTH_CODE_KYE,auth_code);
                        sharedPreferencesCtrl.put(KPNetworkData.AUTH_KYE,auth_key);
                        sharedPreferencesCtrl.put(KPNetworkData.IS_LOGIN_KEY,true);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mWaitingDialog.closeDialog();
                                CommonUtil.showToast(KPLoginActivity.this,"登录成功");
                                doLoginSuccess(auth_code,auth_key);
                                // Intent intent = new Intent();
                                // intent.setClass(KPLoginActivity.this,KPUserCenterActivity.class);
                                //  startActivity(intent);
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    Log.e("data"," errorMsg 111 json data is "+errorMsg);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtil.showToast(KPLoginActivity.this,errorMsg);
                            mWaitingDialog.closeDialog();
                        }
                    });
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                Log.e("data"," login errorMsg is "+errorMsg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtil.showToast(KPLoginActivity.this,errorMsg);
                        mWaitingDialog.closeDialog();
                    }
                });
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(NetworkConfigData.PASSWORD_KEY,password);
            jsonObject.put(NetworkConfigData.USER_KEY,account);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        KPPostRequest postRequest = new KPPostRequest(this,url,jsonObject.toString(),listener);
        postRequest.startAsync();

    }
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode == KPNetworkData.LOGIN_REQUEST){
            if(resultCode == KPNetworkData.REGIST_SUCCESS){
                setResult(KPNetworkData.LOGIN_SUCCESS,data);
                finish();
            }else if(resultCode == KPNetworkData.RESET_PASSWORD_SUCCESS){

            }
        }
    }


    public void onDestroy(){
        super.onDestroy();

    }

 
    protected void doLoginSuccess(String auth_code, String auth_key ){
        Intent intent = new Intent();
        intent.putExtra(KPNetworkData.AUTH_CODE_KYE,auth_code);
        intent.putExtra(KPNetworkData.AUTH_KYE,auth_key);
        setResult(KPNetworkData.LOGIN_SUCCESS,intent);
        finish();
    }
}
